home *** CD-ROM | disk | FTP | other *** search
- Path: news.compulink.co.uk!usenet
- From: John@zeitgeist.compulink.co.uk (John)
- Newsgroups: comp.lang.c++
- Subject: Re: Templates problem: I don't get this :(
- Date: Sat, 02 Mar 1996 19:06:43 GMT
- Organization: PPL
- Message-ID: <4ha639$fr2@zinc.compulink.co.uk>
- References: <4h6unr$3hu@wsintt09.win.tue.nl>
- Reply-To: John@zeitgeist.compulink.co.uk
- NNTP-Posting-Host: zeitgeist.compulink.co.uk
- X-Newsreader: Forte Free Agent 1.0.82
-
- dedos8@wsintt09.win.tue.nl (Arnaud Gouder de Beauregard) wrote:
-
- >I tried to make a Binary Tree with templates, but the following won't
- >compile: What am I doing wrong ?
- Try instead:
-
- template <class T>
- class btree
- {
- public:
- // to set nodeval, need valid = operator !!!
- btree( T& val ) : nodeval( val ), leftNode( 0 ), rightNode( 0 )
- {}
-
- void addNode( T& arg1 )
- {
- if ( arg1 <= nodeval )
- if ( ! leftNode )
- leftNode = new btree( arg1 );
- else
- leftNode->addNode( arg1 );
- else
- if ( ! rightNode )
- rightNode = new btree( arg1 );
- else
- rightNode->addNode( arg1 );
- }
-
- private:
- T nodeval;
- btree* leftNode;
- btree* rightNode;
- };
-
- btree <double> aTree( 10.0 );
-
- void f()
- {
- aTree.addNode( 9.0 );
- }
-
- Note that there is plenty missing from the above and is not intended as an
- example of good coding !
- Better still, re-use an existing btree class, but I guess you're doing this as
- a learning exercise :-)
- Regards,
- John (Freelance technical consultant).
- Regards,
- John.
-
-